<template>
  <el-dialog :title="title" width="800px" :visible.sync="dialogVisible" append-to-body @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="模具" prop="moldId">
            <mold-query ref="moldQuery" v-model="submitForm.moldId" @choose="handleChangeMold" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="分类" prop="categoryId">
            <category-query ref="categoryQuery" v-model="submitForm.categoryId" @choose="handleChangeCategory" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="productName">
            <el-input v-model="submitForm.productName" placeholder="请输入产品名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颜色" prop="color">
            <el-input v-model="submitForm.color" placeholder="请输入颜色" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="材质" prop="material">
            <el-input v-model="submitForm.material" placeholder="请输入材质" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="尺码" prop="size">
            <el-input v-model="submitForm.size" placeholder="请输入尺码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="长度" prop="length">
            <el-input v-model="submitForm.length" placeholder="请输入长度" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="含税单价" prop="unitPrice">
            <el-input-number
              v-model="submitForm.unitPrice"
              :precision="2"
              :step="0.1"
              :max="9999999999"
              style="width: 100%"
              placeholder="请输入含税单价"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位" prop="unit">
            <dict-select v-model="submitForm.unit" dict-key="prod_unit" placeholder="请选择单位" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="重量(克)" prop="weight">
            <el-input v-model="submitForm.weight" type="number" placeholder="请输入重量" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库存数量" prop="stock">
            <el-input v-model="submitForm.stock" type="number" placeholder="请输入库存数量" />
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-form-item label="模具" prop="moldId">
        <mold-query ref="moldQuery" v-model="submitForm.moldId" @choose="handleChangeMold" />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <category-query ref="categoryQuery" v-model="submitForm.categoryId" @choose="handleChangeCategory" />
      </el-form-item>
      <el-form-item label="名称" prop="productName">
        <el-input v-model="submitForm.productName" placeholder="请输入产品名称" />
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-input v-model="submitForm.color" placeholder="请输入颜色" />
      </el-form-item>
      <el-form-item label="材质" prop="material">
        <el-input v-model="submitForm.material" placeholder="请输入材质" />
      </el-form-item>
      <el-form-item label="尺码" prop="size">
        <el-input v-model="submitForm.size" placeholder="请输入尺码" />
      </el-form-item>
      <el-form-item label="长度" prop="length">
        <el-input v-model="submitForm.length" placeholder="请输入长度" />
      </el-form-item>
      <el-form-item label="含税单价" prop="unitPrice">
        <el-input-number
          v-model="submitForm.unitPrice"
          :precision="2"
          :step="0.1"
          :max="9999999999"
          style="width: 100%"
          placeholder="请输入含税单价"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <dict-select v-model="submitForm.unit" dict-key="prod_unit" placeholder="请选择单位" />
      </el-form-item>
      <el-form-item label="重量(克)" prop="weight">
        <el-input v-model="submitForm.weight" type="number" placeholder="请输入重量" />
      </el-form-item>-->
<!--      <el-form-item label="状态" prop="state">
        <el-switch
          v-model="submitForm.state"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="可用"
          inactive-text="禁用"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>-->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </div>
    <div v-if="productAttrVisible">
      <product-attribute-module ref="productAttrModule" :visible="productAttrVisible" @close="handleProductAttrChange" />
    </div>
  </el-dialog>
</template>
<script>

import { saveProduct, selectProductById, updateProductById } from '@/api/erp/product'
import ProductAttributeModule from '@/modules/productAttributeModule.vue'

export default {
  name: 'SubmitModule',
  components: { ProductAttributeModule },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '',
      formLoading: false,
      submitForm: {
        id: '',
        moldId: '',
        moldName: '',
        productName: '',
        unitPrice: 10.00,
        unit: '',
        categoryId: '',
        categoryName: '',
        size: '',
        weight: '',
        stock: '',
        state: '1',
        remark: ''
      },
      submitFormRules: {
        moldId: [{ required: true, message: '请选择模具', trigger: 'change' }],
        productName: [{ required: true, message: '请输入产品名称', trigger: 'change' }],
        unitPrice: [{ required: true, message: '请输入含税单价', trigger: 'change' }],
        unit: [{ required: true, message: '请选择产品单位', trigger: 'change' }],
        size: [{ required: true, message: '请输入尺码', trigger: 'change' }],
        categoryId: [{ required: true, message: '请选择分类', trigger: 'change' }],
        stock: [{ required: true, message: '请输入库存', trigger: 'change' }]
      },
      categoryData: [],
      dialogVisible: this.visible,
      productAttrVisible: false,
      newAttr: []
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.title = '新增产品'
        return
      }
      this.title = '修改产品'
      this.formLoading = true
      selectProductById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
          this.$refs.moldQuery.init(this.submitForm.moldName)
          this.$refs.categoryQuery.init(this.submitForm.categoryId)
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    // 表单提交
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveProduct
          if (this.submitForm.id) {
            request = updateProductById
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.$emit('close')
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleChangeCategory(categoryId) {
      this.submitForm.categoryId = categoryId
    },
    handleChangeMold(row) {
      if (row) {
        this.submitForm.moldId = row.id
        this.$refs.moldQuery.init(row.moldName)
        this.productAttrVisible = true
        this.$nextTick(() => {
          this.$refs.productAttrModule.init(row)
        })
      }
    },
    handleProductAttrChange(row) {
      this.productAttrVisible = false
      console.log(row)
      if (row) {
        this.submitForm.color = row.color
        this.submitForm.material = row.material
        this.submitForm.size = row.size
        this.submitForm.length = row.length
        this.submitForm.stock = row.quantity
        this.submitForm.unitPrice = row.unitPrice
        this.submitForm.weight = row.weight
        this.submitForm.unit = row.unit
        this.submitForm.categoryId = row.categoryId
        this.submitForm.productName = row.moldName + '*' + row.color + '*' + row.material + '*' + row.size
        this.$nextTick(() => {
          this.$refs.categoryQuery.init(row.categoryId)
        })
      }
    },
    // 表单关闭
    handleClose() {
      this.$refs.submitForm.resetFields()
      this.$emit('close')
    }
  }
}
</script>
